//
//  AnimationLabel.m
//  AnimationLabel
//
//  Created by 正合适 on 2017/10/27.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "AnimationLabel.h"

#define RGBAColor(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:a]
#define RGBColor(r,g,b)    RGBAColor((r),(g),(b),1)

@interface AnimationLabel ()
/**<#desc#>*/
@property (nonatomic, strong) UILabel *mainLabel;
/**<#desc#>*/
@property (nonatomic, copy) NSString *initalTitle;

@end

@implementation AnimationLabel
- (instancetype)initWithFrame:(CGRect)frame initalTitle:(NSString *)title {
    self = [super initWithFrame:frame];
    if (self) {
        _initalTitle = title;
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
    _mainLabel = [[UILabel alloc] initWithFrame:self.bounds];
    _mainLabel.textColor = RGBColor(51, 51, 51);
    _mainLabel.font = [UIFont systemFontOfSize:14];
    _mainLabel.text = _initalTitle;
    [self addSubview:_mainLabel];
}

- (void)configTextColor:(UIColor *)color {
    _mainLabel.textColor = color;
}

#pragma mark - 赋值动画效果-上下翻页
- (void)configAnimationAWithText:(NSString *)text {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -_mainLabel.frame.size.height, _mainLabel.frame.size.width, _mainLabel.frame.size.height)];
    label.textColor = _mainLabel.textColor;
    label.font = _mainLabel.font;
    label.text = text;
    label.alpha = 0;
    [self addSubview:label];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.5 animations:^{
        weakSelf.mainLabel.frame = CGRectMake(0, weakSelf.mainLabel.frame.size.height, weakSelf.frame.size.width, weakSelf.mainLabel.frame.size.height);
        weakSelf.mainLabel.alpha = 0;
        label.frame = weakSelf.bounds;
        label.alpha = 1;
    } completion:^(BOOL finished) {
        weakSelf.mainLabel.frame = weakSelf.bounds;
        weakSelf.mainLabel.text = text;
        weakSelf.mainLabel.alpha = 1;
        [label removeFromSuperview];
    }];
}

@end
